<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" @click="addVisible = true" size="mini"
          >添加分类</el-button
        >
      </div>
      <el-table style="width: 100%" :data="cateList" border stripe>
        <el-table-column
          label="序号"
          type="index"
          width="100"
        ></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="primary" size="mini" @click="showEditDialog(row)"
              >修改</el-button
            >
            <el-button @click="delEditDialog(row)" type="danger" size="mini"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- :before-close="handleClose"  关闭前的回调函数-->
    <!-- 新增 -->
    <el-dialog
      @closed="$refs.addForm.resetFields()"
      title="添加文章分类"
      :visible.sync="addVisible"
      width="35%"
    >
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addForm"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addHandler"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog
      @closed="$refs.editForm.resetFields()"
      title="修改文章分类"
      :visible.sync="editVisible"
      width="35%"
    >
      <el-form
        :model="editForm"
        :rules="addRules"
        ref="editForm"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="editHandler"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 删除 -->
    <el-dialog title="提示" :visible.sync="delVisible" width="30%">
      <span>确认删除吗?</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="delHandler">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      addVisible: false,
      editVisible: false,
      delVisible: false,
      delId: '',
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          {
            required: true,
            message: '请填写分类名称',
            trigger: 'blur'
          },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的分空字符串',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          {
            required: true,
            message: '请填写分类别名',
            trigger: 'blur'
          },
          {
            pattern: /^[0-9a-zA-Z-_]{1,15}$/,
            message: '分类名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getArtCate()
  },
  methods: {
    async getArtCate () {
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code === 0) {
        this.cateList = res.data
      }
    },
    addHandler () {
      this.$refs.addForm.validate(async (result) => {
        if (!result) return
        const { data: res } = await this.$http.post(
          '/my/cate/add',
          this.addForm
        )
        if (res.code === 0) {
          this.addVisible = false
          this.getArtCate()
        }
      })
    },
    showEditDialog (row) {
      if (row.id === 1 || row.id === 2) {
        return this.$message.error('管理员不允许修改这个分类！')
      }
      this.editVisible = true
      this.editForm = { ...row }
    },
    delEditDialog (row) {
      if (row.id === 1 || row.id === 2) {
        return this.$message.error('管理员不允许删除这个分类！')
      }
      this.delVisible = true
      this.delId = row.id
    },
    editHandler () {
      this.$refs.editForm.validate(async (result) => {
        if (!result) return
        const { data: res } = await this.$http.put(
          '/my/cate/info',
          this.editForm
        )
        if (res.code === 0) {
          this.editVisible = false
          this.getArtCate()
        }
      })
    },
    async delHandler () {
      const { data: res } = await this.$http.delete('/my/cate/del', {
        params: {
          id: this.delId
        }
      })
      if (res.code === 0) {
        this.delVisible = false
        this.getArtCate()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
